//----------------------------------------------------------------------------
// inputcmd.cpp
//
// display dialog allowing user to input simple text
//
// Copyright (C) 2011 Neil Butterworth
//----------------------------------------------------------------------------

#include <windows.h>
#include <string>
#include <iostream>
#include "command.h"
#include "cmdline.h"
#include "util.h"
#include "resource.h"

using namespace std;

namespace Swine {

//----------------------------------------------------------------------------
// Command & option names
//----------------------------------------------------------------------------

const char * const INPUT_CMD 		= "input";
const char * const CAPTION_OPT 		= "caption";
const char * const TEXT_OPT 		= "text";
const char * const HIDE_OPT 		= "hide";

//----------------------------------------------------------------------------
// Help
//----------------------------------------------------------------------------

const char * const INPUT_HELP =
	"input single line of text\n"
	"options:\n"
	" --caption     specify message box caption (default is 'Enter Text') \n"
	" --text        initial text for input edit (default is empty string)\n"
	" --hide        if used, input is masked with asterisks\n";

//----------------------------------------------------------------------------
// Command class and registration
//----------------------------------------------------------------------------

class InputCmd : public Command {

	public:

		InputCmd() : Command( INPUT_CMD, INPUT_HELP ) {}

		BOOL ProcessControlMessages( HWND wnd, int ctrl, int msg ) const;
		int Execute( CmdLine & cl ) const;
		void InitDialog( HWND dlg, CmdLine & cl  ) const;

};

static AddCommand <InputCmd> cmd1_;

//----------------------------------------------------------------------------
// Handle WM_COMMAND messages - we are only interested in button clicks
//----------------------------------------------------------------------------

BOOL InputCmd :: ProcessControlMessages( HWND hwnd, int ctrl, int msg ) const {

	if ( ctrl == IDCANCEL && msg == BN_CLICKED ) {
		Reply( "" );
		DestroyWindow( hwnd );
		return TRUE;
	}
	else if ( ctrl == IDOK && msg == BN_CLICKED ) {
		Reply( TextFromCtrl( hwnd, IDC_INPUTEDIT ) );
		DestroyWindow( hwnd );
		return TRUE;
	}
	else {
		return TRUE;
	}
}

//----------------------------------------------------------------------------

int InputCmd :: Execute( CmdLine & cl ) const {
	DoDialog( IDD_INPUTDLG, cl );
	return 0;
}

//----------------------------------------------------------------------------

void InputCmd :: InitDialog( HWND dlg, CmdLine & cl ) const {
	string caption = cl.ExtractOption( CAPTION_OPT, "Enter Text" );
	string text = cl.ExtractOption( TEXT_OPT, "" );
	cl.MustBeEmpty();

	bool hide = cl.ExtractOption( HIDE_OPT );
	if ( ! hide ) {
		SendMessage( MustGetControl( dlg, IDC_INPUTEDIT  ), EM_SETPASSWORDCHAR, 0, 0 );
	}
	SetWindowText( dlg, caption.c_str() );
	SetWindowText( MustGetControl( dlg, IDC_INPUTEDIT  ), text.c_str() );
	SetFocus( dlg, IDC_INPUTEDIT );
	SendMessage( MustGetControl( dlg, IDC_INPUTEDIT  ), EM_SETSEL, 0, -1 );
	cl.MustBeEmpty();
}

} // namespace
